<template>
  <div class="admin-tag-catalog">
    <el-card>
      <tag-content-header search refresh setting @toolFun="toolFun" :settingData="columns">
        <template #title>
          <span>标签体系列表</span>
        </template>
      </tag-content-header>
      <tag-table
        ref="table"
        @refresh="getList"
        :data="dataList"
        :loading="loading"
        :columns="columns"
        :params="params"
        :total="totalSize"
      >
        <template #annc="{scope}">
          <span class="circle" :class="{ red: !scope.row.annc }"></span>{{ scope.row.annc ? '是' : '否' }}
        </template>
        <template #catalogClassType="{scope}">
          <span>
            <icon-svg :name="scope.row.catalogClassType | iconFormatter" class="svg-catalog"></icon-svg>
            {{ typeName[scope.row.catalogClassType] }}
          </span>
        </template>
        <template #operation="{ scope }">
          <div class="operation-button-box">
            <el-tooltip effect="dark" content="卸载所有标签" placement="top" :enterable="false">
              <icon-svg name="iconDelete" class="svg-delete" @click.native="handleDelete(scope.row)"></icon-svg>
            </el-tooltip>
          </div>
        </template>
      </tag-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      dataList: [],
      columns: [
        {
          show: true,
          label: '标签体系名称',
          prop: 'catalogClassNm',
          search: true,
          searchKey: ''
        },
        {
          show: true,
          label: '归属',
          prop: 'belongDeptNm',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: '发布范围',
          prop: 'anncRangeCd',
          dict: 'catalog_annc_range',
          dot: {
            1: '#4e5aa4',
            2: '#7ac7c1',
            3: '#8ac0ee'
          },
          align: 'center'
        },
        {
          show: true,
          label: '描述',
          prop: 'catalogClassDesc',
          align: 'center'
        },
        {
          show: true,
          label: '发布日期',
          prop: 'anncDt',
          align: 'center'
        },
        {
          show: true,
          label: '体系类型',
          prop: 'catalogClassType',
          align: 'center'
        },
        {
          show: true,
          label: '挂载标签数',
          prop: 'mountCount',
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 200
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0,
      typeName: {
        0: '普通体系',
        1: '基础体系',
        2: '场景体系',
        3: '触达体系',
        4: '画像体系',
        5: '事件标签'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('auth/admin/classPageList'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          if (data.data.currentPageNum === this.params.currentPageNum) {
            this.dataList = data.data.records
            this.totalSize = data.data.totalSize
          }
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleDelete(row) {
      this.$confirm('确认卸载体系【' + row.catalogClassNm + '】下的所有标签？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl(`auth/admin/${row.catalogClassId}`),
            method: 'delete',
            params: this.$ajax.setParams()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.getList()
              this.$message.success('操作成功')
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {})
    }
  },
  filters: {
    iconFormatter(icon) {
      let iconName = ''
      switch (icon) {
        case 0:
          iconName = 'iconCommon'
          break
        case 1:
          iconName = 'iconBasic'
          break
        case 2:
          iconName = 'iconScene'
          break
        case 3:
          iconName = 'iconTouch'
          break
        case 4:
          iconName = 'iconPicture'
          break
        case 5:
          iconName = 'iconEvent'
          break
      }
      return iconName
    }
  }
}
</script>

<style lang="scss">
.admin-tag-catalog {
  .operation-button-box {
    .svg-mount {
      color: #f6bf1b;
    }
  }
  .svg-catalog {
    width: 20px;
    height: 20px;
    color: $theme-color;
    vertical-align: middle;
    margin-top: -5px;
  }
}
</style>
